package com.mall.order.services;

import com.mall.order.OrderQueryService;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * cskaoyan
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Override
    public OrderListResponse orderList(OrderListRequest request) {

        //新建返回数据对象
        OrderListResponse response = new OrderListResponse();
        //获取userId，复用
        Long userId = request.getUserId();

        //新建根据userId查找订单的example
        Example orderExample = new Example(Order.class);
        orderExample.createCriteria().andEqualTo("user_id", userId);

        //查找出所有订单
        List<Order> orders = orderMapper.selectByExample(orderExample);

        //新建一个用来填装返回数据的list
        ArrayList<OrderDetailInfo> orderDetailInfos = new ArrayList<>();

        for (Order order : orders) {
            //复用orderId
            String orderId = order.getOrderId();

            OrderDetailInfo orderDetailInfo = getOrderDetailInfoByOrderId(orderId);

            orderDetailInfos.add(orderDetailInfo);
        }

        response.setDetailInfoList(orderDetailInfos);
        response.setTotal((long) orderDetailInfos.size());

        return response;
    }

    @Override
    public OrderDetailResponse orderDetail(OrderDetailRequest request) {
        OrderDetailResponse response = new OrderDetailResponse();

        String orderId = request.getOrderId();

        OrderDetailInfo orderDetailInfo = getOrderDetailInfoByOrderId(orderId);

        BeanUtils.copyProperties(orderDetailInfo, response);

        return response;
    }

    private OrderDetailInfo getOrderDetailInfoByOrderId(String orderId) {
        //根据orderId查找另外两个数据
        List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);

        //新建返回容器
        OrderDetailInfo orderDetailInfo = new OrderDetailInfo();

        //新建返回dto成员
        ArrayList<OrderItemDto> orderItemDtos = new ArrayList<>();
        OrderShippingDto orderShippingDto = new OrderShippingDto();

        //迁移
        BeanUtils.copyProperties(orderItems, orderItemDtos);
        BeanUtils.copyProperties(orderShipping, orderShippingDto);

        //set
        orderDetailInfo.setOrderItemDto(orderItemDtos);
        orderDetailInfo.setOrderShippingDto(orderShippingDto);

        //返回
        return orderDetailInfo;
    }
}
